package com.guoteng.androidlearn;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ListView listView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = new ListView(this);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));

        setContentView(listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getApplicationContext(), position + "-" + id, Toast.LENGTH_SHORT).show();

                switch (position) {

                    case 0: {

                        Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
                        intent.putExtra("data_return", "爱你大萌儿");

//                        startActivity(intent);
                        startActivityForResult(intent, 1);
                        break;
                    }
                    case 1: {

                        Intent intent = new Intent(MainActivity.this, ButtonActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 2: {

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse("zgt://www.baidu.com"));
                        startActivity(intent);
                        break;
                    }
                    case 3:{

                        Intent intent = new Intent(MainActivity.this, LifeCircleActivity.class);
                        startActivity(intent);
                        break;
                    }

                    case 4: {

                        Intent intent = new Intent(MainActivity.this, FirstActivity.class);
                        startActivity(intent);

                        break;
                    }

                    case 5: {

                        CommonUIActivity.startActivity(MainActivity.this);

                        break;
                    }

                    case 6: {

                        LayoutActivity.startActivity(MainActivity.this);

                        break;
                    }

                    case 7: {

                        CustomLayoutActivity.startActivity(MainActivity.this);

                        break;
                    }

                    case 8: {

                        ListActivity.startActivity(MainActivity.this);
                        break;
                    }

                    case 9: {

                        // RecyclerView
                        RecyclerActivity.startActivity(MainActivity.this);

                        break;
                    }

                    default:

                        Log.d("MainActivity", "onItemClick");
                        break;
                }

            }
        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private List<String> getData() {

        List<String> data = new ArrayList<>();

        data.add("TextView");
        data.add("Button");
        data.add("隐式意图");
        data.add("Activity 生命周期");
        data.add("Activity 启动模式");
        data.add("常用UI");
        data.add("四种基本布局");
        data.add("自定义布局");
        data.add("ListView");
        data.add("RecyclerView");
        data.add("5");
        data.add("6");
        data.add("7");
        data.add("8");
        data.add("9");

        return data;
    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

            case 1:

                Toast.makeText(getApplicationContext(), data.getStringExtra("back"), Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
    
}
